﻿Imports System.Linq
Imports Equipment.Core
Imports Equipment.DataAccess

Public Class SiteDepartmentManager
    Public Shared Function GetAllFor(ByVal forUser As IUserInfo) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            Return SiteDepartment.RetrieveAll()
        Else
            Return SiteDepartment.RetrieveAllDepartmentsBySite(forUser.SiteID)
        End If
    End Function

    Public Shared Function GetAllDepartmentsFor(ByVal forUser As IUserInfo) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            Return SiteDepartment.RetrieveAll(SiteDepartmentType.Department)
        Else
            Return SiteDepartment.RetrieveAllDepartmentsBySite(forUser.SiteID)
        End If
    End Function

    Public Shared Function GetAllDepartmentsBySiteFor(ByVal siteID As Integer, ByVal forUser As IUserInfo) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            Return SiteDepartment.RetrieveAll(SiteDepartmentType.Department).Where(Function(d) d.ParentID = siteID)
        Else
            Return SiteDepartment.RetrieveAllDepartmentsBySite(forUser.SiteID)
        End If
    End Function

    Public Shared Function RetrieveAllDepartmentsBySite(ByVal siteID As Integer) As IEnumerable(Of SiteDepartment)
        Return SiteDepartment.RetrieveAllDepartmentsBySite(siteID)
    End Function

    Public Shared Function GetDepartmentTreeFor(ByVal forUser As IUserInfo) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            Return SiteDepartment.RetrieveAll(SiteDepartmentType.Department)
        Else
            Return SiteDepartment.RetrieveAllDepartmentsBySite(forUser.SiteID)
        End If
    End Function

    Public Shared Function GetAllSitesFor(ByVal forUser As IUserInfo) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            Return SiteDepartment.RetrieveAll(SiteDepartmentType.Site)
        Else
            Return SiteDepartment.RetrieveAllDepartmentsBySite(forUser.SiteID)
        End If
    End Function

    Public Shared Function GetTreeFor(ByVal forUser As IUserInfo) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            Return SiteDepartment.GetTree()
        Else
            Return SiteDepartment.GetTreeForSite(forUser.SiteID)
        End If
    End Function

    Public Shared Function GetTreeFor(ByVal forUser As IUserInfo, ByVal filter As String) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            Return SiteDepartment.GetTree(filter)
        Else
            Return SiteDepartment.GetTreeForSite(forUser.SiteID, filter)
        End If
    End Function

    Public Shared Function SearchDepartmentByNameFor(ByVal forUser As IUserInfo, ByVal prefixText As String) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            Return SiteDepartment.SearchDepartmentByName(prefixText)
        Else
            Return SiteDepartment.SearchDepartmentByNameForSite(forUser.SiteID, prefixText)
        End If
    End Function

#Region "FilterForCreateWorkOrder"

    Public Shared Function GetAllSiteWithAssetFor(ByVal forUser As IUserInfo) As IEnumerable(Of SiteDepartment)
        If forUser.IsSuperUser Then
            'return all active site
            Dim results As New List(Of SiteDepartment)

            For Each item As SiteDepartment In SiteDepartment.RetrieveAll(SiteDepartmentType.Site)
                Dim active As List(Of Asset) = Asset.RetrieveAllBySiteID(item.ID).ToList()
                If active.Count <> 0 Then
                    results.Add(item)
                End If
            Next
            Return results
        Else
            'return site for User default
            Return SiteDepartment.GetByPrimaryKey(forUser.SiteID)
        End If
    End Function

    Public Shared Function GetDepartamentsWithAssetForSite(ByVal siteId As Integer) As IEnumerable(Of SiteDepartment)
        Dim results As New List(Of SiteDepartment)

        For Each departament As SiteDepartment In SiteDepartmentManager.RetrieveAllDepartmentsBySite(siteId)
            Dim active As List(Of Asset) = Asset.RetrieveAll(0, 0, departament.ID).ToList()
            For Each asset As Asset In active
                If Not results.Contains(departament) Then
                    results.Add(departament)
                End If
            Next
        Next
        Return results
    End Function


#End Region

End Class
